package com.trsj.lnsj.controller;

import com.trsj.lnsj.common.DataResponse;
import com.trsj.lnsj.model.ErrorResponse;
import com.trsj.lnsj.service.imp.EaseGroupService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 
 * <p>
 * Title: EaseGroupController.java
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2018
 * </p>
 * 
 * <p>
 * Company: 七色环
 * </p>
 * 
 * @author jiaxb
 * 
 * @date 2019年3月30日
 * 
 * @version 1.0
 * 
 */
@RestController
@Api(tags = {"环信群组相关api"})
@RequestMapping(value = "/api/easegroup/*")
@CrossOrigin
public class EaseGroupController {
    @Autowired
    private EaseGroupService easegroupService;


    /**
     * 查询一个群组黑名单中的用户列表。位于黑名单中的用户查看不到该群组的信息，也无法收到该群组的消息。
     * @param groupId
     * @return
     */
    @ApiOperation(value = "APP应用-查询群组黑名单|V1.0.0|Add")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", paramType = "path", dataType = "string", required = true, defaultValue = "", value = "群组号")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 400 , message = "", response = ErrorResponse.class)
    })
    @GetMapping(value = "/queryBlockUsers/{group_id}")
    public DataResponse queryBlockUsers(@PathVariable String groupId) {
        DataResponse resp = easegroupService.queryBlockUsers(groupId);
        return resp;
    }

    /**
     * 修改成功的数据行会返回 true，失败为 false。请求 body 只接收 groupname、description、maxusers　三个属性，传其他字段会被忽略。
     * 注意：修改 groupname ​和 description ​时，如果需要使用空格，请用“+”替换。如需要写“test group1”，请用“test+group1”代替
     * @param groupId
     * @param groupName
     * @param description
     * @param maxusers
     * @return
     */
    @ApiOperation(value = "APP应用-修改群组信息 |V1.0.0|Add")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", dataType = "String", defaultValue = "", required = true, value = "群组ID"),
            @ApiImplicitParam(name = "groupname", dataType = "String", defaultValue = "",value = "群组名称"),
            @ApiImplicitParam(name = "description", dataType = "String", defaultValue = "", value = "群组描述"),
            @ApiImplicitParam(name = "maxusers", dataType = "int", defaultValue = "", required = false, value = "群组成员最大数"),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 400 , message = "", response = ErrorResponse.class)
    })
    @PutMapping(value = "/updateGroup/{group_id}")
    public DataResponse updateGroup(@PathVariable String groupId,String groupName, String description, Integer maxusers) {
        DataResponse resp = easegroupService.updateGroup(groupId, groupName, description, maxusers);
        return resp;
    }

    @ApiOperation(value = "APP应用-删除群组 |V1.0.0|Add")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", dataType = "String", defaultValue = "", required = true, value = "群组ID")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 400 , message = "", response = ErrorResponse.class)
    })
    @DeleteMapping(value = "/deleteGroup/{group_id}")
    public DataResponse deleteGroup(@PathVariable String groupId) {
        DataResponse resp = easegroupService.deleteGroup(groupId);
        return resp;
    }

    /**
     * 获取一个或多个群组的详情
     * @param groupIds
     * @return
     */
    @ApiOperation(value = "APP应用-获取一个或多个群组的详情|V1.0.0|Add")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = Constants.VERSION_STRIKE_KEY, paramType = "header", dataType = "int", defaultValue = "0", value = "接口修正版本[可选]"),
            @ApiImplicitParam(name = "groupIds", paramType = "path", dataType = "string", defaultValue = "", value = "可以输入多个groupid，用,隔开")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 400 , message = "", response = ErrorResponse.class)
    })
    @GetMapping(value = "/groupDetail/chatgroups/{groupIds}")
    public DataResponse getGroupsDetail(@PathVariable(name="groupIds",required = true) String groupIds) {
        DataResponse resp = easegroupService.getGroupsDetail(groupIds);
        return resp;
    }

    /**
     *
     * @param limit
     * @param cursor
     * @return
     */
    @ApiOperation(value = "APP应用-获取APP中所有的群组（可分页）|V1.0.0|Add")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = Constants.VERSION_STRIKE_KEY, paramType = "header", dataType = "int", defaultValue = "0", value = "接口修正版本[可选]"),
            @ApiImplicitParam(name = "limit", paramType = "query", dataType = "string", defaultValue = "10", value = "预期获取的记录数。如果不填写，则获取全部的群组"),
            @ApiImplicitParam(name = "cursor", paramType = "query", dataType = "string", defaultValue = "", value = "请求上一页之后返回的cursor。如果不填写则返回第一页。")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 400 , message = "", response = ErrorResponse.class)
    })
    @GetMapping(value = "/app/allGroups")
    public DataResponse getAppAllGroups(@RequestParam(name="limit",defaultValue = "10") String limit, @RequestParam(name="cursor",required = false) String cursor) {
        DataResponse resp = easegroupService.getAppAllGroups(limit,cursor);
        return resp;
    }

    /**
     *
     * @param groupname
     * @param desc
     * @param isPublic
     * @param maxusers
     * @param approval
     * @param owner
     * @return
     */
    @ApiOperation(value = "APP应用-创建一个群组|V1.0.0|Add")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = Constants.VERSION_STRIKE_KEY, paramType = "header", dataType = "int", defaultValue = "0", value = "接口修正版本[可选]"),
            @ApiImplicitParam(name = "groupname", paramType = "query", dataType = "string",required = true,defaultValue = "", value = "群组名称"),
            @ApiImplicitParam(name = "desc", paramType = "query", dataType = "string", required = true,defaultValue = "", value = "群组描述"),
            @ApiImplicitParam(name = "isPublic", paramType = "query", dataType = "boolean", required = true,defaultValue = "true", value = "是否是公开群"),
            @ApiImplicitParam(name = "maxusers", paramType = "query", dataType = "int", defaultValue = "200", value = "群组成员最大数（包括群主）"),
            @ApiImplicitParam(name = "approval", paramType = "query", dataType = "boolean", required = true,defaultValue = "false", value = "加入公开群是否需要批准，默认值是false"),
            @ApiImplicitParam(name = "owner", paramType = "query", dataType = "string", required = true,defaultValue = "", value = "群组的管理员")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 400 , message = "", response = ErrorResponse.class)
    })
    @PostMapping(value = "/createSingleEaseGroup")
    public DataResponse createSingleEaseGroup(String groupname, String desc, boolean isPublic, int maxusers, boolean approval, String owner) {
        DataResponse resp = easegroupService.createSingleEaseGroup(groupname,desc,isPublic,maxusers,approval,owner);
        return resp;
    }


    @ApiOperation(value = "APP应用-从群组黑名单移除用户[批量] |V1.0.0|Add")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", dataType = "String", defaultValue = "", value = "群组ID"),
            @ApiImplicitParam(name = "usernames", dataType = "String", defaultValue = "", value = "用户ID，多个用户使用','分割")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 400 , message = "", response = ErrorResponse.class)
    })
    @DeleteMapping(value = "/app/blocks/{groupId}/users/{usernames}")
    public DataResponse deleteBlocksUsers(@PathVariable String groupId,@PathVariable String usernames) {
        DataResponse resp = easegroupService.deleteBlocksUsers(groupId, usernames);
        return resp;
    }

    /**
     * 添加多个用户进入一个群组的黑名单，一次性最多可以添加60个用户。
     * @param groupId
     * @param usernames
     * @return
     */
    @ApiOperation(value = "APP应用-添加用户至群组黑名单[批量]|V1.0.0|Add")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", paramType = "path", dataType = "String", defaultValue = "", value = "群组ID"),
            @ApiImplicitParam(name = "usernames", paramType = "query", dataType = "String", defaultValue = "", value = "用户名[多个用户名用,隔开]")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 400 , message = "", response = ErrorResponse.class)
    })
    @PostMapping(value = "/app/blocks/{groupId}/users")
    public DataResponse addBlocksUsers(@PathVariable String groupId, String usernames) {
        DataResponse resp = easegroupService.addBlocksUsers(groupId, usernames);
        return resp;
    }


    @ApiOperation(value = "APP应用-指定群组的所有成员 |V1.0.0|Add")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", dataType = "String", defaultValue = "", value = "群组ID")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 400 , message = "", response = ErrorResponse.class)
    })
    @GetMapping(value = "/app/group/{groupId}/users")
    public DataResponse getGroupUsers(@PathVariable String groupId) {
        DataResponse resp = easegroupService.getGroupUsers(groupId);
        return resp;
    }


    @ApiOperation(value = "APP应用-移除群组成员 |V1.0.0|Add")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", dataType = "String", defaultValue = "", value = "群组ID"),
            @ApiImplicitParam(name = "usernames", dataType = "String", defaultValue = "", value = "用户ID，多个用户使用','分割")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 400 , message = "", response = ErrorResponse.class)
    })
    @DeleteMapping(value = "/chatgroups/{group_id}")
    public DataResponse deleteGroupUsers(@PathVariable String groupId, @PathVariable String usernames) {
        DataResponse resp = easegroupService.deleteGroupUsers(groupId, usernames);
        return resp;
    }

    /**
     * 为群组添加多个成员，一次最多可以添加60位成员
     * @param groupId
     * @param username
     * @return
     */
    @ApiOperation(value = "APP应用-添加群组成员[批量]|V1.0.0|Add")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", dataType = "String", defaultValue = "", value = "群组ID"),
            @ApiImplicitParam(name = "usernames", dataType = "String", defaultValue = "", value = "用户ID，多个用户使用','分割")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 400 , message = "", response = ErrorResponse.class)
    })
    @PostMapping(value = "/app/group/{groupId}/users/{username}")
    public DataResponse addGroupUsers(@PathVariable String groupId, @PathVariable String username) {
        DataResponse resp = easegroupService.addGroupUsers(groupId, username);
        return resp;
    }

    @ApiOperation(value = "APP应用-转让群组 |V1.0.0|Add")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", dataType = "String", defaultValue = "", value = "群组ID")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 400 , message = "", response = ErrorResponse.class)
    })
    @PutMapping(value = "/app/group/{groupId}")
    public DataResponse transferGroup(@PathVariable String groupId, @RequestBody String newowner) {
        DataResponse resp = easegroupService.transferGroup(groupId, newowner);
        return resp;
    }

}
